#include "volumetool.h"
#include "ui_volumetool.h"

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    // 窗口添加自定义的阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);
    shadowEffect->setXOffset(0.0);
    this->setGraphicsEffect(shadowEffect);

    // 设置按钮图标
    ui->slienceBtn->setIcon(QIcon(":/images/volumn"));
    // 将默认音量设置为20%
    ui->volumeRatio->setText("20%");
    // 调整大小
    QRect rect = ui->outLine->geometry();
    ui->outLine->setGeometry(rect.x(),180 - 36 + 25,rect.width(),36);
    // 还要移动按钮
    ui->sliderBtn->move(ui->sliderBtn->x(),ui->outLine->y() - ui->sliderBtn->height() / 2);

    connect(ui->slienceBtn,&QPushButton::clicked,this,&VolumeTool::onSlienceBtnClikced);

    ui->widget->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{

    // 绘制界面下三角
    QPainter painter(this);
    // 1.设置画笔
    painter.setPen(Qt::NoPen);
    // 2.设置画刷
    painter.setBrush(QBrush(Qt::white));
    // 3.绘制三角形
    QPolygon polygon;
    QPoint a(10+10,300);
    QPoint b(10 + 10 + 60,300);
    QPoint c(10 + 10 + 30,300+20);
    polygon.append(a);
    polygon.append(b);
    polygon.append(c);
    painter.drawPolygon(polygon);


}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(ui->widget == watched)
    {
        // 事件发生在该控件上
        if(event->type() == QEvent::MouseButtonPress)
        {
            // 捕捉到按下事件
            calcVolume();
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            // 捕捉到松开事件
            emit setMusicVolume(volumeRatio);
        }
        else if(event->type() == QEvent::MouseMove)
        {
            // 鼠标移动事件
            calcVolume();
            emit setMusicVolume(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(watched,event);
}

void VolumeTool::calcVolume()
{
    // 计算音量大小

    // Get the global cursor position as a QPoint
    QPoint globalCursorPos = QCursor::pos();

    // Map the global cursor position to the widget's local coordinate system
    QPoint localCursorPos = ui->widget->mapFromGlobal(globalCursorPos);

    // Extract the Y-coordinate (height) relative to the widget
    int height = localCursorPos.y();
    height = height < 25 ? 25:height;
    height = height > 205? 205:height;
    ui->outLine->setGeometry(ui->outLine->x(),height,ui->outLine->width(),205-height);
    ui->sliderBtn->move(ui->sliderBtn->x(),ui->outLine->y() - ui->sliderBtn->height() / 2);
    volumeRatio = (int)(ui->outLine->height() / (float)180 * 100);
    // 更新画面

    // 更新百分比
    ui->volumeRatio->setText(QString::number(volumeRatio)+"%");
}

void VolumeTool::onSlienceBtnClikced()
{
    isMuted = !isMuted;
    // 媒体静音设置 发送信号给StarMusic处理
    if(isMuted)
    {
        ui->sliderBtn->setIcon(QIcon(":/images/silent.png"));
    }
    else
    {
        ui->sliderBtn->setIcon(QIcon(":/images/volumn.png"));
    }
    emit setMusicMuted(isMuted);
}
